package com.capgemini.teamtwo.controller;


import com.capgemini.teamtwo.common.ResponseBean;
import com.capgemini.teamtwo.entity.UserLogin;
import com.capgemini.teamtwo.entity.vo.RegVO;
import com.capgemini.teamtwo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@Api("用户管理")
@RestController
@RequestMapping("/user")
public class UserController extends BaseController{

    @Autowired
    UserService userService;


    @ApiOperation("用户注册接口")
    @PostMapping("/reg")
    public ResponseBean userReg(@RequestBody RegVO regVO) {
        // 调用注册逻辑
        userService.userReg(regVO);
        return ResponseBean.success("注册成功");

    }

    /**
     * 用户登陆接口
     * @param userLogin 表单用户登陆对象
     * @param session 用户数据
     * @return 当前登陆用户的详细信息
     */
    @ApiOperation("用户登陆接口")
    @PostMapping("/login")
    public ResponseBean<UserLogin> login(@RequestBody UserLogin userLogin, HttpSession session){
        UserLogin data = userService.login(userLogin);
        session.setAttribute("id",data.getId());
        session.setAttribute("username",data.getUsername());
        return ResponseBean.success(data);

    }
}
